package com.myway.service.ibe.common;

import com.myway.dto.domain.OfficeInfo;
import com.myway.dto.domain.Segment;
import com.myway.dto.domain.TotalFlyPrice;
import com.myway.dto.ibe_param.TaxPrice;
import com.myway.dto.ibe_param.TicketPrice;
import com.myway.dto.vo.FlySegmentVo;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by yufei on 2018/6/9.
 */
public class PNRFC {


    public static String toStringFC(TicketPrice ticketPrice, List<FlySegmentVo> segments, boolean infant) {
        /*****************************
         ** 积分支付时：FC 按实际支付价格计算
         **/
        DecimalFormat format = new DecimalFormat("###############0.00");
        double totalPrice = 0;
        StringBuffer s = new StringBuffer();
        s.append("FC/");
        if (infant) {
            s.append("IN/");
        }
        int i = 0;
        for (FlySegmentVo segment : segments) {
            if (i == 0) {
                s.append(segment.getDepCode()).append(" ");
            }
            s.append(OfficeInfo.AIRLINE).append(" ")
                    .append(segment.getArrCode()).append(" ")
                    .append(format.format(ticketPrice.getBaseFareAmount()))
                    .append(segment.getCabinClass() + " ");
            totalPrice += ticketPrice.getBaseFareAmount();
            i++;
        }
        s.append(ticketPrice.getBaseFareCurrencyCode()).append(format.format(totalPrice)).append("END");

        //TODO:如果税费大于三项要加入XT
        if (ticketPrice.getTaxPrices().size() > 3) {
            s.append(" XT ");
            //计算第二项税费之后的税费之和
            for (int j = 2; j < ticketPrice.getTaxPrices().size(); j++) {
                TaxPrice taxprice = ticketPrice.getTaxPrices().get(j);
                if (ticketPrice.getTaxPrices().get(j).isEXEMPT() == true || ticketPrice.getTaxPrices().get(j).getAmount() == 0.0) {
                    s.append(" EXEMPT" + taxprice.getTaxCode());
                } else {
                    s.append(ticketPrice.getTaxPrices().get(j).getCurrencyCode()).append(format.format(ticketPrice.getTaxPrices().get(j).getAmount())).append(ticketPrice.getTaxPrices().get(j).getTaxCode());
                }
            }
        }
        return s.toString();
    }

    public static String toStringFC(TotalFlyPrice totalFlyPrice, List<TaxPrice> taxPrices, List<FlySegmentVo> segments, boolean infant) {
        /*****************************
         ** 积分支付时：FC 按实际支付价格计算
         **/
        DecimalFormat format = new DecimalFormat("###############0.00");
        double totalPrice = 0;
        String currencyCode = segments.get(0).getCurrency();
        String travlType = segments.get(0).getTravlType();
        StringBuffer s = new StringBuffer();
        s.append("FC/");
        if (infant) {
            s.append("IN/");
        }
        int i = 0;
        for (FlySegmentVo segment : segments) {
            if (i == 0) {
                s.append(segment.getDepCode()).append(" ");
            }
            double singlePrice = totalFlyPrice.getAdultAmount();
            if (infant) singlePrice = totalFlyPrice.getBabayAmount();
            if (travlType.equals("RT")) singlePrice = singlePrice / 2;
            s.append(OfficeInfo.AIRLINE).append(" ")
                    .append(segment.getArrCode()).append(" ")
                    .append(format.format(singlePrice))
                    .append(segment.getCabinClass() + " ");
            totalPrice += totalFlyPrice.getAdultAmount();
            i++;
        }
        if (travlType.equals("RT")) totalPrice = totalFlyPrice.getAdultAmount();
        s.append(currencyCode).append(format.format(totalPrice)).append("END ");

        double totalTax = 0.0;
        if (taxPrices.size() > 0) {
            for (TaxPrice taxPrice : taxPrices) {
                double tax = taxPrice.getAmount();
                totalTax += tax;
            }
            //总税费
//            s.append("/X").append(currencyCode).append(format.format(totalTax));
        }
        //TODO:如果税费大于三项要加入XT
        if (taxPrices.size() > 3) {
            s.append(" XT ");
            //计算第二项税费之后的税费之和
            for (int j = 2; j < taxPrices.size(); j++) {
                TaxPrice taxprice = taxPrices.get(j);
                if (taxPrices.get(j).isEXEMPT() == true || taxPrices.get(j).getAmount() == 0.0) {
                    s.append(" EXEMPT" + taxprice.getTaxCode());
                } else {
                    s.append(taxPrices.get(j).getCurrencyCode()).append(format.format(taxPrices.get(j).getAmount())).append(taxPrices.get(j).getTaxCode());
                }
            }
        }
        return s.toString();
    }

    public static String toStringFC(TotalFlyPrice totalFlyPrice, List<TaxPrice> taxPrices, List<FlySegmentVo> segments, boolean infant, boolean child) {
        /*****************************
         ** 积分支付时：FC 按实际支付价格计算
         **/
        DecimalFormat format = new DecimalFormat("###############0.00");
        String currencyCode = segments.get(0).getCurrency();
        String travlType = segments.get(0).getTravlType();
        StringBuffer s = new StringBuffer();
        s.append("FC/");
        if (infant) {
            s.append("IN/");
        }
        int i = 0;
        double totalPrice = totalFlyPrice.getAdultAmount();
        if (child) totalPrice = totalFlyPrice.getChildrenAmount();
        if (infant) totalPrice = totalFlyPrice.getBabayAmount();
        for (FlySegmentVo segment : segments) {
            if (i == 0) {
                s.append(segment.getDepCode()).append(" ");
            }
            double singlePrice = totalPrice;
            double backPrice = 0.00;
            if (travlType.equals("RT")) {
                singlePrice = Double.parseDouble(format.format(singlePrice / 2));
                if (i == 1) {
                    backPrice = totalPrice - singlePrice;
                }
            }

            s.append(OfficeInfo.AIRLINE).append(" ")
                    .append(segment.getArrCode()).append(" ");
            if (i == 1) {
                s.append(format.format(backPrice));
            } else {
                s.append(format.format(singlePrice));
            }
            s.append(segment.getCabinClass() + " ");
//            totalPrice += totalFlyPrice.getAdultAmount();
            i++;
        }
        s.append(currencyCode).append(format.format(totalPrice)).append("END ");

        double totalTax = 0.0;
        if (taxPrices.size() > 0) {
            for (TaxPrice taxPrice : taxPrices) {
                double tax = taxPrice.getAmount();
                totalTax += tax;
            }
            //总税费
//            s.append("/X").append(currencyCode).append(format.format(totalTax));
        }
        //TODO:如果税费大于三项要加入XT
        if (taxPrices.size() > 3) {
            s.append(" XT ");
            //计算第二项税费之后的税费之和
            for (int j = 2; j < taxPrices.size(); j++) {
                TaxPrice taxprice = taxPrices.get(j);
                if (taxPrices.get(j).isEXEMPT() == true || taxPrices.get(j).getAmount() == 0.0) {
                    s.append(" EXEMPT" + taxprice.getTaxCode());
                } else {
                    s.append(taxPrices.get(j).getCurrencyCode()).append(format.format(taxPrices.get(j).getAmount())).append(taxPrices.get(j).getTaxCode());
                }
            }
        }
        return s.toString();
    }
}
